home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
Cream of the Crop 22
/
Cream of the Crop 22.iso
/
program
/
recio215.zip
/
USAGE.TXT
< prev
Wrap
Text File
|
1996-10-26
|
37KB
|
1,003 lines
Title: STANDARD USAGE OF C LANGUAGE RECIO LIBRARY
Copyright: (C) 1994-1996, William Pierpoint
Version: 2.15
Date: October 26, 1996
1.0 INTRODUCTION
The implementation descibed by this standard usage is a superset of the
recio specification. Enhancements are noted in the text.
1.1 Mnemonics
The recio functions have been given a consistent mnemonic naming
convention. All recio functions are in lower case and start with
the letter r. Function names are analogous to <stdio.h> functions.
Mnemonics are as follows:
Single letter (field functions) Multi-letter
---------------------------------------- -----------------
b - base (prefix) beg - beginning
c - column (prefix), character (suffix) ch - character
d - double (suffix) col - column
f - float (suffix) cxt - context
i - integer (suffix) eof - end of file
l - long (suffix) err - error
n - number fld - field buffer
r - record pointer (first letter) fmt - format
s - string pointer (suffix) fn - function
t - time_t time (suffix) no - number
u - unsigned (suffix) num - number
rec - record buffer
siz - size of buffer
str - string
tm - struct tm time
txt - text
1.2 Order
The order in which the prefix mnemonics appear indicates the order in which
the arguments appear in the function. The suffix mnemonics of the input
functions tell you what the function returns. The suffix mnemonics of the
output functions indicate the last argument and tell you what the function
outputs. All output functions return an integer with a zero value if the
function executed successfully.
For example, the input function rbgetui():
arguments: r - record pointer
b - base (radix)
returns: ui - unsigned integer
The output function rbputui():
arguments: r - record pointer
b - base (radix)
ui - unsigned integer is output
Note: c is used in the prefix of a function's name only once even if there are
two column arguments. If the function inputs or outputs a character,
there is only one column argument; otherwise there are two.
2.0 ERRORS AND WARNINGS
The functions declared in the header <recio.h> make use of the errno macro
defined in section 4.1.3 of ANSI X3.159-1989. This mechanism was chosen
because (1) the <stdlib.h> conversion functions (strtod(), strtol(), etc.)
make use of this error reporting mechanism and (2) the <recio.h> functions
make use of the <stdlib.h> conversion functions.
In this implementation, errno can return the following macro constants:
0 - No error.
EACCES - permission denied.
EDOM - argument out of domain.
EINVAL - invalid argument.
EMFILE - too many open files.
ENOENT - no such file or directory.
ENOMEM - out of memory.
ERANGE - out of range.
where
* EACCESS means that you don't have permission to access this file. All
MSDOS files have read permission.
* EDOM says that the argument is ouside of the defined domain. For
example, if the days of the month are defined as 1 to 31 and you
convert a string "1/32/1995" using sftotm(), an EDOM error will occur.
* EINVAL indicates an invalid argument to a function, usually a NULL record
pointer. This is most often caused by a programming error.
* EMFILE means the program tried to open more files than the maximum allotted
by ROPEN_MAX or FOPEN_MAX. If your program is interactive, the user can
close one or more open record streams. Or you might decide that ROPEN_MAX
or FOPEN_MAX needs to be a larger value.
* ENOENT says that ropen() could not find the requested file to open.
Perhaps the name of the file was misspelled, or your program looked in
wrong directory. If your program was trying to read a configuration file,
it could use internal default values when the configuration file does
not exist.
* ENOMEM indicates that the program ran out of heap space. You may be able
to correct this if you are able to deallocate memory you no longer need.
For example, you could reduce the size of buffers when the size only
affects speed. Such buffers need to be flushed first. Buffers used by
the recio library do not fit this criteria.
* ERANGE tells you that the data is outside the capabilities of the data
type to represent it. The data value is either too large or too small.
Either the data is incorrect or you need a more robust data type to hold
the data.
Beginning with version 1.1, recio functions set errno when the record
pointer is invalid and set an internal error number when the record pointer
is valid. The recio error number is accessed through the rerror function.
The rerror function can return the following macro constants:
0 - No error.
R_EDOM - out of domain.
R_EINVAL - invalid argument (not the record pointer).
R_EINVDAT - invalid data.
R_EINVMOD - invalid mode.
R_EMISDAT - missing data.
R_ENOMEM - out of memory.
R_ENOGET - input record too long.
R_ENOPUT - unable to write data to output.
R_ERANGE - out of range.
R_EFAULT - application defined error.
where
* R_EDOM indicates an argument to a function is outside the defined
domain. For example, if base is defined to have a valid range of
2 - 32 and you pass a 1, an R_EDOM error occurs.
If the following conditions are not met, an R_EDOM error will result:
Input of integral numbers: 2 <= base <= 32 or base==0
Output of integral numbers: 2 <= base <= 32
Input of time: 1 <= month <= 12
1 <= day <= 31
0 <= hour <= 23
0 <= minute <= 59
0 <= second <= 61 (includes 2 leap seconds)
* R_EINVDAT says the data is invalid. Invalid data is caused by an
unrecognized character in the field. For example, if you read an
integral number that contained a letter, a R_EINVDAT error would
occur.
* R_EINVMOD indicates that you opened a file in read mode, then used an
output function or opened a file in write or append mode, then used an
input function.
* R_EMISDAT says the data is missing. Missing data means the field is empty.
If you expect a number, you could substitute either zero or some unique
number to indicate an empty field.
* R_ENOMEM indicates that the program ran out of heap space. You may be able
to correct this if you are able to deallocate memory you no longer need.
For example, you could reduce the size of buffers when the size only
affects speed. Such buffers need to be flushed first. Buffers used by
the recio library do not fit this criteria.
* R_ENOGET is caused by a record being longer than size_t. Most probable
cause is from trying to read a binary file.
* R_ENOPUT says the program was unable to write the data to the output.
One possible cause is that the disk is full.
* R_ERANGE tells you that the data is outside the capabilities of the
data type to represent it. For instance, suppose you used rgeti()
to get an integer and the data value is 32768. If a 16-bit integer
has an upper limit of 32767, the value is too large. If the data is
wrong, you can have the error function correct it. If the data is
right, you need to correct the data type within the program.
If the following types of data are not within the following ranges,
a R_ERANGE error will occur:
integer: INT_MIN <= x <= INT_MAX
long: LONG_MIN <= x <= LONG_MAX
unsigned integer: 0 <= x <= UINT_MAX
unsigned long: 0 <= x <= ULONG_MAX
float: -FLT_MAX <= x <= -FLT_MIN or FLT_MIN <= x <= FLT_MAX or x==0.0
double: -DBL_MAX <= x <= -DBL_MIN or DBL_MIN <= x <= DBL_MAX or x==0.0
time_t: TIME_T_MIN <= x <= TIME_T_MAX
* R_EFAULT can be used with rseterr() in your own application.
The recio library also has warning numbers. There is not a global warning
number equivalent to errno. The warning indicator is associated with a
record stream. Thus warnings can only be set for valid record streams.
The warning number for a record stream is accessed through the rwarning
function.
The rwarning function can return the following macro constants:
0 - No warning.
R_WEMPSTR - empty data string.
R_WNOREG - unable to register exit function with atexit().
R_WWIDTH - data too wide for columnar output.
R_WTMFMT - incomplete data in a time field.
R_WFAULT - application defined warning.
where
* R_WEMPSTR says that an empty data string was input. If you want to
substitute another string from within your program, use the rsetfldstr
function in your callback warning function to place the new string
into the field buffer.
* R_WWIDTH indicates that on output, the data will not fit between the
columns specified. If the data is numeric, the recio output functions
write asterisks to the output; if the data is a string, a truncated string
is written.
* R_WNOREG means the program was unable to register the internal recio exit
function with the ANSI atexit() function. The internal recio exit
function ensures that all open record streams are closed and all dynamic
memory allocated by the recio library is deallocated.
* R_WTMFMT means the data in a time field was incomplete. For example,
the time format may have been looking for a date and a time of day but
the time field only contained a date. Data is only compared left to
right. Any mismatch in the delimiters that separate the time elements
(month, hour, etc) generates an R_EINVDAT error.
* R_WFAULT can be used with rsetwarn() in your own applications.
2.1 Define Callback Error Function
First define a callback error function to be used by the recio functions.
You may give the function any name you wish. In the sample function below,
the name rerrfn is used. The function takes one argument, a record pointer
(REC *). It returns nothing (void). The function must first check for a
valid record pointer using the risvalid function. Other than that, you can
customize it to do whatever you want.
A simple callback error function rerrmsg is included in the RECIO library.
In the initial prototyping stages of development, you may wish to use this
function rather than taking the time to develop your own function. Later
you can substitute a more robust callback error function.
The recio functions use a callback error function in order to give the most
flexibility in handling errors. The rerrfn function shown below just sends
information to stderr. You may wish to send information to a printer, a
file, a window, or a dialog box. You might even want to give users the
ability to examine errors and enter corrections. If the error is corrected,
you will want to call the rclearerr function before your callback error
function returns.
When your callback error function is invoked, check rerror() or errno
to determine the cause of the error.
The main purpose of this sample callback error function is to show some of
kinds of things you can do in a callback error function. Note that when an
error occurs, the column number indicator rcolno() has moved just beyond
the error. To make it clearer to the user where the error occurred, rerrfn()
displays rcolno()-1, but not less than rbegcolno(), the column number for the
first column.
A more detailed callback error function is given in the source code for the
test program TESTCHG.C. The test program callback error function makes use
of the rfix functions to fix up bad data (primarily overflows and underflows)
and continue processing. If appropriate for your application, you can use
these functions as well. They have been compiled into the recio libraries
for your potential use.
/* define callback error function */
void rerrfn(REC *rp)
{
/* if rp is a valid record pointer */
if (risvalid(rp)) {
/* if reof indicator set */
if (reof(rp)) {
fprintf(stderr, "ERROR reading %s -- "
"tried to read past end of file\n\n", rnames(rp));
/* else rerror indicator set */
} else {
/* determine cause of error */
switch (rerror(rp)) {
/* input data errors */
case R_ERANGE:
case R_EINVDAT:
case R_EMISDAT:
/* output data errors */
case R_ENOPUT:
/* print location of error */
fprintf(stderr, "DATA ERROR in FILE %s at LINE %ld,"
" FIELD %u, COLUMN %u -- %s\n", rnames(rp), rrecno(rp),
rfldno(rp), max(rcolno(rp)-1, rbegcolno(rp)), rerrstr(rp));
break;
/* fatal errors (R_EINVMOD, R_EINVAL, R_ENOMEM) */
default:
fprintf(errout, "FATAL ERROR reading FILE %s -- %s\n",
rnames(rp), rerrstr(rp));
abort();
break;
}
}
/* else invalid record pointer */
} else {
switch (errno) {
/* non-fatal errors */
case EACCES:
case EMFILE:
fprintf(errout, "WARNING: %s\n", strerror(errno));
break;
/* fatal errors (EINVAL, ENOMEM) */
default:
fprintf(errout, "FATAL ERROR: %s\n", strerror(errno));
abort();
break;
}
}
}
2.2 Define Callback Warning Function
Next define a callback warning function. You may give the function any name
you want. In the sample function below, the name rwarnfn is used. The
function takes one argument, a record pointer (REC *). It returns nothing
(void).
A simple callback warning function rwarnmsg is included in the recio library.
In the initial prototyping stages of development, you may wish to use this
function rather than taking the time to develop your own function. Later
you can substitute a more robust callback warning function.
The recio functions use a callback warning function in order to give the
most flexibility in handling unusual conditions. For example, the recio
library considers empty data strings to be legal but your application may
want to flag an empty data string as a data error. You can do that by
checking for R_WEMPSTR warnings in your callback warning function.
When your callback warning function is invoked, check rwarning() to
determine the cause of the warning.
You can also use your callback warning function to keep track of the number
of warnings, then print a summary of any warnings just before you close a
record stream. You will find an example in the test program TESTCHP.C.
void rwarnfn(REC *rp)
{
if (risvalid(rp)) {
switch (rwarning(rp)) {
case R_WNOREG: /* atexit() full */
fprintf (errout, "WARNING %s\n", rwarnstr(rp));
break;
case R_WEMPSTR: /* empty data string */
case R_WWIDTH: /* data too wide for columns */
case R_WTMFMT: /* time data incomplete */
fprintf(errout, "WARNING reading %s at record %lu and field %u -- %s\n",
rnames(rp), rrecno(rp), rfldno(rp), rwarnstr(rp));
break;
}
}
}
2.3 Register Callback Error and Warning Functions
Once you have written your callback error and warning functions, you must let
the other recio functions know that they exist. You use the rseterrfn and
rsetwarnfn functions to individually register your callback functions or
the rinit to register both functions at once. You may find use of rinit
easier to remember.
/* register callback error and warning functions */
rseterrfn(rerrfn);
rsetwarnfn(rwarnfn);
OR
/* register callback error and warning functions */
rinit(rerrfn, rwarnfn);
OR to use the simple built-in callback error/warning functions
/* register built-in callback error and warning functions */
rinit(rerrmsg, rwarnmsg);
OR to use the simple built-in error function without a warning function
/* register callback error function */
rinit(rerrmsg, NULL);
3.0 OPEN FILE
3.1 Open File and Get Record Pointer
Use the ropen function to open the file you want to read, write, or append.
Store the record pointer returned by the ropen function. Do not open recin,
recout, recerr, or recprn (MSDOS printer). They are always open, so they do
not need to be opened or closed.
REC *rp = ropen("FILENAME.DAT", "r");
3.2 Check Record Pointer
Following the ropen function, you need to check to see if the file was
opened correctly. If ropen returned a NULL pointer, then the file was not
opened.
Errors other than ENOENT are reported to your callback error function.
ENOENT is not reported since you may want to use default values if the
data file is not available.
/* if ropen() failed */
if (!rp) {
/* if it failed because file does not exist */
if (errno==ENOENT) {
/* action to take when file does not exist */
...
}
/* else ropen() succeeded */
} else {
/* set up stream (see sections 3.3 - 3.6) */
...
/* read or write file (see sections 4 and 5) */
...
/* close file (see section 6) */
rclose(rp);
}
3.3 Set Field and Text Delimiters and Time Format
The space character is the default value for both the field and text
delimiters. The space character matches any white space. The default
time format string is "%m/%d/%y". If you need to something else, use
the rsetfldch, rsettxtch, and rsettmfmt functions to explicitly set the
values. Application maintenance will be easier if you get in the habit
of setting these values for each record stream.
rsetfldch(rp, ','); /* set field delimiter character */
rsettxtch(rp, '"'); /* set text delimiter character */
rsettmfmt(rp, "%m/%d/%Y"); /* set time format */
3.4 Set Field and Record Buffer Sizes
Setting the field and record buffer sizes is optional. Buffers will be
automatically reallocated as necessary. However if you set the field and
record sizes in advance to the maximum value needed, you can reduce memory
fragmentation. The field and record buffers are not used for output streams.
rsetfldsiz(rp, 41); /* set size of field buffer */
rsetrecsiz(rp, 133); /* set size of record buffer */
3.5 Set Context Number
If your application opens record streams with more than one data format, you
will want to set a context number. You use the context number so that your
callback error function can determine (using the rcxtno function) which data
format it is dealing with. Each context number must be a positive integer;
zero and negative numbers are reserved. Predefined context symbolic
constants are RECIN, RECOUT, RECERR, and (for MSDOS) RECPRN.
#define SOILS_DB 1
#define BUILDINGS_DB 2
rsetcxtno(rp, SOILS_DB); /* set context number */
3.6 Set Beginning Column Number
The first column number in the record buffer defaults to zero. If you prefer
column numbering to start at one, use the rsetbegcolno function. It is mainly
useful if using column delimited data. If a number takes up the first ten
columns of the record, the column numbering will be 0 to 9 if rsetbegcolno()
is set to 0, or 1 to 10 is rsetbegcolno() is set to 1.
rsetbegcolno(rp, 1); /* first column is column 1 */
3.7 Set Beginning Year
If you are going to read time data (time_t or struct tm) using the %y
time format, you may need to set the beginning year. The %y format
represents the year as a two digit number. Thus the year 76 may represent
1776 if your application deals with the signing of the U.S. Declaration
of Independence, or 1976 if your application deals with the 20th Century.
The default beginning year is controlled by RECBEGYR and is set in recio.h
to 1951. Thus the default range for the %y format corresponds to the years
1951 to 2050.
rsetbegyr(1980); /* %y range 1980 to 2079 */
Note that the rsetbegyr function applies globally to the application;
it does not contain a record stream argument.
You could also have an application that sets the beginning year dynamically.
The implementation below is restricted by the range of time_t.
time_t curtime; /* current time */
struct tm *curtmp; /* pointer to current tm */
if (time(&curtime) != (time_t) -1) {
curtmp = localtime(&curtime);
rsetbegyr(curtmp->tm_year + 1900 - 50); /* current year - 50 years */
} else {
rseterr(NULL, EDOM);
}
4.0 RECORD FUNCTIONS
4.1 The rgetrec Function
If all the records in a data file have the same format, you will want to
loop through all the records until the end of file is reached. If each
record has a different format, you must call the rgetrec function each
time you want to get the next record. Calling rgetrec() is optional for
the first record.
/* read all records in file */
while (rgetrec(rp)) {
/* Section 5 field functions go here ... */
}
4.2 The rputrec Function
After you write all the fields in one record, use the rputrec function
to put the end-of-record newline character to the output and to reset the
internal recio library variables for the next record.
/* write end-of-record */
rputrec(rp);
4.3 The rrecs Macro
To get a pointer to the start of the record buffer, use the rrecs macro.
/* echo record contents to stdout */
printf("%s\n", rrecs(rp));
4.4 The rrecno Macro
To get the record number, use the rrecno macro.
/* echo record number and record contents to stdout */
printf("%ld: %s\n", rrecno(rp), rrecs(rp));
4.5 The rsetrecstr Function
There may be times when you will find it useful to stuff the record buffer
with your own string, then use the field input functions to scan the fields.
Use the rsetrecstr function for this.
4.6 The rresetrec Macro
The rresetrec macro resets internals so that you can start reading fields
from the beginning of the record buffer. This allows you to scan through
the fields in the record buffer multiple times.
5.0 GET AND PUT FIELD DATA
5.1 Field functions
There are 70 functions that can be used to read or write data. A mnemonic
system makes it easy to construct the name of any function you need. Here
are the rules:
1. There are six prefixes, two bodies, and up to ten suffixes.
Prefix Body Suffix Prefix Body Suffix
------ ---- ------ ------ ---- ------
r get c rb get i
rc put d rcb put l
rn f rnb ui
i ul
l
s
t
tm
ui
ul
2. The rn and rnb prefixes are only used with the get functions.
3. If the prefix contains the letter 'c', it is a column delimited field
function.
4. If the prefix contains the letter 'n', it is a character delimited
field function that lets you jump directly to the field number before
reading the data.
5. If the prefix contains the letter 'b', it is a field function that
reads or writes an integral number in a specified base (radix).
6. The get body is used to read data, the put body is used to write data.
7. The ten suffixes indicate the ten data types: character, double, float,
integer, long, string, time_t, struct tm, unsigned integer, and unsigned
long.
5.2 The rskipfld Macro and rskipnfld Function
If your application does not need to read the data in a field, you can skip
over the field by using the rskipfld macro.
rskipfld(rp);
If your application does not need to read the data in several adjacent
fields, you can skip over the fields by using the rskipnfld function.
/* skip over three fields */
rskipnfld(rp, 3);
5.3 The rgotofld Function
If your application needs to jump around to different fields in a
character delimited record, use the rgotofld function. You can jump
to fields in any order.
The first field in a data record is field number 1. However don't
confuse the field number here with rfldno. They are not always the same.
You can understand this better by referring to the diagram below for a
comma delimited record with three fields.
data: 1 , 2 , 3
rgotofld: 1 2 3 4
rfldno: 0 1 1 1 1 2 2 2 2 3 3 3 3
processing: rgeti rgeti rgeti
The multiple values for rfldno in the diagram represent how rfldno
changes in value as the fields in the record are read sequentially
using three rgeti calls.
If you use rgotofld to go to field 1, you go to rfldno 0. As the data
in field 1 is processed using an rget function, rfldno quickly changes
to 1. If an error occurs while processing the first field, rfldno will be
1 and can be used within the error or warning callback function to
indicate which field had an error. Thus in the context of an error or
warning callback function, the field number and rfldno are the same,
but in the context of the rgotofld function, the field number is greater
than rfldno by 1.
Note that although this record has 3 fields, you can go to field 4. If
you attempt to go to any field beyond the last field in the record, you
will be at the end of the record. No error occurs until you attempt
to get the data from the field using an rget function.
/* go to field number 3 */
rgotofld(rp, 3);
Since you probably are going to read the field after moving to it, you
can use the rnget and rnbget series of functions to move to a field
and read from it in one step.
5.4 The ristxtfld Macro
You can use the ristxtfld macro to determine if the current field (the field
most recently input or output) was quoted with the text delimiter character
(provided the text delimiter character is not the space character). For
example, if you need to write out a string field in the same format in
which was read, you can use this macro to determine if the original field
was quoted.
/* write field in same format as read */
rgets(recin);
if (ristxtfld(recin)) rsettxtch(recout, '"');
else rsettxtch(recout, ' ');
rputs(recout, rflds(recin));
5.5 The reof Macro
Use the reof macro to determine when the record stream has reached the
end of file.
/* if error or end of file reached */
while (rgetrec(rp)) {
...
}
/* if end of file */
if (reof(rp)) {
...
/* else error */
} else {
...
}
5.6 The rerror Function
Use the rerror function to determine if an error has occurred on a record
stream. You can access a text string for the error using the rerrstr
function.
if (rerror(rp))
printf("ERROR reading %s - %s\n",
rnames(rp), rerrstr(rp));
rclose(rp);
It is a good practice to check for any errors just before closing
a record stream. If the error indicator is clear, you have additional
confidence that the stream was processed correctly.
5.7 The rseterr Function
If you write wrapper functions or other functions that interact with
recio functions, your code will need to handle errors. If can use
the rseterr function to set the error number and to call the record
stream callback error function.
/* get integer and validate range */
int rrgeti(REC *rp, int min, int max) {
int result;
result = rgeti(rp);
if (result < min || result > max) {
rseterr(rp, R_ERANGE);
}
return result;
}
5.8 The rwarning Function
Use the rwarning macro to determine if a warning has occurred on a record
stream.
if (rwarning(rp)) {
printf("ERROR %s - %s\n", rnames(rp), rwarnstr(rp));
}
It is good practice to check for any warnings just before closing an output
record stream.
5.9 The rsetwarn Function
If you write wrapper functions or other functions that interact with recio
functions, you may need to provide warnings. The rsetwarn function sets the
warning number and calls the record stream callback warning function.
rsetwarn(rp, R_WWIDTH);
5.10 The rnumfld Function
You can determine the number of character delimited fields in the current
record of an input record stream with the rnumfld function.
unsigned numfld;
numfld = rnumfld(recin);
5.11 The rgetfldpos and rsetfldpos Functions
If you want to mark a specific position within the current record and later
return to the same position, use the rgetfldpos and rsetfldpos functions.
If the record number has changed between calls, a R_EINVAL error is generated
and passed to the callback error function. The pos argument is passed by
reference, but a macro takes care of placing the & for you.
rpos_t pos;
rgetrec(recin);
rgetfldpos(recin, pos); /* get bookmark at start of record */
rskipnfld(recin, 3); /* skip three fields */
rsetfldpos(recin, pos); /* reset to bookmark position */
6.0 CLOSE FILE
6.1 Close File
When finished reading or writing a data file, close it. Do not close recin,
recout, recerr, or recprn as they are always open.
/* close record file */
rclose(rp);
6.2 Close All Files
Rather than closing record files one at a time, one can close all open
record files at once using the rcloseall function.
/* all done */
rcloseall();
7.0 STRING FUNCTIONS
The recio library contains a small set of string functions. Ideally these
would be part of a larger separate string library, but are included here
because the recio functions need them. You may also find them useful for
your application.
7.1 Trim a string
The scntrimbegs, scntrimends, and scntrims functions allow you to trim the
same character from the beginning, end, and both ends of string up to the
maximum number specified by the last argument. The sctrimbegs, sctrimends,
and sctrims functions let you to trim all multiple occurances of the same
character from the beginning, end, and both ends of a string. If you
specify the space character, all whitespace is trimmed. You can use the
strimbegs, strimends, and strims macros to trim whitespace from the ends.
char str[]="\t Hello, World?!!!\n";
strims(str); /* trim whitespace from both ends of string */
sctrimends(str, '!'); /* then trim all ! chars from end of string */
scntrimends(str, '?', 1); /* finally trim one ? char from end of string */
7.2 Dynamically copy and concatenate a string
The scpys and scats macros are used to dynamically copy and concatenate a
string. To use these macros you must adhere to these points:
1. Initialize pointers of destination strings to NULL.
2. These macros pass the destination string by reference (and place
the & for you).
3. A pointer to the destination string is returned.
4. Free any dynamically allocated strings when finished with them.
char *dst=NULL;
char src1[]="Hello ";
char src2[]="world";
scpys(dst, src1);
puts(scats(dst, src2)); /* "Hello world" */
/* however the following will not work here as in C++ where you can
* declare a function to pass an argument by reference, i.e.
* char *scats(char *&dst, const char *src)
*
* puts(scats(scpys(dst, src1), src2)); /* C++ */
*/
free(dst);
8.0 TIME CONVERSION FUNCTIONS
The recio library contains a few time conversion functions.
8.1 Convert from string to time_t or struct tm types.
You can convert a string to either the time_t or struct tm time types using
the sftotime or sftotm functions. These functions complement the ANSI
X3.159-1989 strftime function. However sftotime and sftotm use a subset
of the specifiers found with strftime. The recio library uses the sftotime
and sftotm functions to get time from the input stream, but uses the strftime
function to put time to the output stream. As a consequence, you can use the
complete set of strftime specifiers for any output you do not later have to
read back into a program. If there is an error, the struct tm type will
contain the time "01/01/70 00:00:00" and the time_t type will contain the
value (time_t)-1, i.e. cast -1 to a time_t type.
time_t time;
struct tm t;
time = sftotime("11/24/94", "%m/%d/%y");
t = sftotm("11/24/94", "%m/%d/%y");
8.2 Convert between time_t and struct tm types.
You can convert between the time_t and struct tm types using the tmtotime and
timetotm functions. The functions are similar to the mktime and localtime
functions in ANSI X3.159-1989, but the arguments don't use pointers and they
may be easier to remember. If there is an error, the struct tm type will
contain the time "01/01/70 00:00:00" and the time_t type will contain the
value (time_t)-1, i.e. cast -1 to a time_t type.
time_t time;
struct tm t;
time = sftotime("12/25/94", "%m/%d/%y");
t = timetotm(time);
9.0 REFERENCES
1. ANSI X3.159-1989. American National Standard for Information Systems -
Programming Language - C. American National Standards Institute,
11 West 42nd Street, New York, NY 10036, 1990.
10.0 INDEX
errno macro ............ 2.0, 2.1, 3.2
rbegcolno macro ........ 2.1
rclearerr function ..... 2.1
rclose function ........ 6.1
rcloseall function ..... 6.2
rcolno macro ........... 2.1
rcxtno macro ........... 3.5
recin expression ....... 3.1, 6.1
reof macro ............. 2.1, 5.5
rerror function ........ 2.1, 5.6
rerrmsg function ....... 2.1, 2.3
rerrstr function ....... 2.1, 5.6
rfix functions ......... 2.1
rflds macro ............ 5.4
rfldno macro ........... 2.1
rbget functions ........ 5.1
rbput functions ........ 5.1
rcbget functions ....... 5.1
rcbput functions ....... 5.1
rcget functions ........ 5.1
rcput functions ........ 5.1
rget functions ......... 5.1
rgetfldpos function .... 5.11
rgetrec function ....... 4.1
rgotofld function ...... 5.3
rinit function ......... 2.3
ristxtfld macro ........ 5.4
risvalid function ...... 2.1
rnames macro ........... 2.1
rnbget functions ....... 5.1
rnget functions ........ 5.1
rnumfld function ....... 5.10
ropen function ........ 3.1
rput functions ......... 5.1
rrecs macro ............ 2.1, 4.3
rrecno macro ........... 2.1, 4.4
rresetrec macro ........ 4.6
rsetbegcolno function .. 3.6
rsetbegyr function ..... 3.7
rsetcxtno function ..... 3.5
rseterr function ....... 5.7
rseterrfn function ..... 2.3
rsetfldch function ..... 3.3
rsetfldpos function .... 5.11
rsetfldsiz function .... 3.4
rsetfldstr function .... 2.1
rsetrecsiz function .... 3.4
rsetrecstr function .... 4.5
rsettmfmt function ..... 3.3
rsettxtch function ..... 3.3, 5.4
rsetwarn function ...... 5.9
rsetwarnfn function .... 2.3
rskipfld macro ........ 5.2
rskipnfld function ..... 5.2
rwarning function ...... 2.2, 5.8
rwarnmsg function ...... 2.2, 2.3
rwarnstr function ...... 2.2, 5.7
scats macro ............ 7.2
scpys macro ............ 7.2
sctrimbegs function .... 7.1
sctrimends function .... 7.1
sctrims function ....... 7.1
sftotime function ...... 8.1
sftotm function ........ 8.1
strimbegs macro ........ 7.1
strimends macro ........ 7.1
strims macro ........... 7.1
timetotm function ...... 8.2
tmtotime function ...... 8.2